<template>
    <el-card class="w-full pt-[5px]">
        <el-form :model="searchForm" class="mt-[20px]" label-width="110px">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-form-item label="期刊名称" class="mb-[0px]">
                        <el-input v-model="searchForm.name" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="主办单位">
                        <el-input v-model="searchForm.chargeUnit" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="ISSN号">
                        <el-input v-model="searchForm.issn" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <div class="flex justify-end">
                        <el-button type="primary" @click="getList(1)">查询</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>
    </el-card>
    <el-card class="w-full pt-[5px] mt-[10px]">
        <div class="mb-[20px]">
            <el-button type="primary" @click="handleAdd">
                <el-icon class="mr-[5px]">
                    <Plus />
                </el-icon>
                新增
            </el-button>
            <el-button @click="handleDel">
                <el-icon>
                    <DeleteFilled />
                </el-icon>
                删除
            </el-button>
            <el-button @click="goBack">
                返回
            </el-button>
        </div>
        <el-table :data="tableData" class="w-full" @selection-change="handleSelectionChange" stripe>
            <el-table-column type="selection" width="55" />
            <el-table-column prop="name" label="期刊名称"></el-table-column>
            <el-table-column prop="chargeUnit" label="主办单位" />
            <el-table-column prop="issn" label="ISSN号" />
            <el-table-column fixed="right" label="操作" width="300" align="center">
                <template #default="scope">
                    <el-button type="primary" size="small" plain @click="handleEdit(scope.row)">
                        编辑
                    </el-button>
                    <el-button type="danger" size="small" plain @click="delHonor(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-[20px] flex justify-end">
            <pagination v-show="total > 0" :total="total" v-model:page="pageInfo.pageNum"
                v-model:limit="pageInfo.pageSize" @pagination="getList" />
        </div>
    </el-card>
    <EditModal v-model="editModelVisible" ref="EditModalRef" @callBack="callBackFun" />
</template>

<script setup>
import { onMounted } from 'vue';
import { getCategory } from "@/api/system/category"
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { getmagazineList, magazineDel, getmagazineInfo } from "@/api/achievement/kycg"
import EditModal from "./EditModal.vue";
const router = useRouter();
const route = useRoute();
const editModelVisible = ref(false);
const searchForm = ref({
    name: "",
    chargeUnit: "",
    issn: "",
    magazineSourceId: route.query.id || "",
});
const pageInfo = ref({
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = ref([]);
//点击新增
const handleAdd = () => {
    editModelVisible.value = true;
};
const EditModalRef = ref(null)
//点击编辑
const handleEdit = (i) => {
    getmagazineInfo(i.id).then(res => {
        if (res.code == 200) {
            editModelVisible.value = true;
            EditModalRef.value.getRuleForm(res.data)
        }
    })
};



const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
//点击删除
const handleDel = () => {
    if (multipleSelection.value.length == 0) {
        ElMessage.warning("请选择要删除的数据")
        return;

    } else {
        ElMessageBox.confirm("是否确认删除？", "提示", {
            type: "warning",
        }).then(() => {
            let ids = multipleSelection.value.map(item => item.id)
            ids = ids.join(',')
            magazineDel(ids).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })
        });
    }

};


const initList = () => {
    getList()
}

const getList = async (i) => {
    if (i == 1) {
        pageInfo.value.pageNum = 1
    }
    let parms = {
        ...searchForm.value,
        ...pageInfo.value
    }
    let res = await getmagazineList(parms)
    if (res.code == 200) {
        tableData.value = res.rows
        total.value = res.total
        console.log(tableData.value)
    }
}
const callBackFun = () => {
    getList()
}
const goBack = () => {
    router.back()

}
const delHonor = (i) => {
    ElMessageBox.confirm(
        '确认！删除这条数据?',
        '提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            magazineDel(i.id).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })


        })
        .catch(() => {

        })

}
onMounted(() => {
    initList()
});
</script>

<style scoped lang="scss"></style>